package com.example.docmanagement.controller;


import com.example.docmanagement.dto.request.CustomerCreateRequest;
import com.example.docmanagement.dto.request.CustomerQueryRequest;
import com.example.docmanagement.dto.request.CustomerUpdateRequest;
import com.example.docmanagement.dto.response.ApiResponse;
import com.example.docmanagement.dto.response.CustomerResponse;
import com.example.docmanagement.dto.response.PageResponse;
import com.example.docmanagement.entity.Customer;
import com.example.docmanagement.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/customers")
@RequiredArgsConstructor
@Tag(name = "客户管理", description = "客户的增删改查接口")
public class CustomerController {

    private final CustomerService customerService;

    /**
     * 获取客户列表
     */
    @GetMapping
    @Operation(summary = "获取客户列表", description = "获取客户列表,支持分页、筛选、排序")
    public ResponseEntity<ApiResponse<PageResponse<CustomerResponse>>> getCustomers(
            @ParameterObject @ModelAttribute CustomerQueryRequest request){
        PageResponse<CustomerResponse> customers = customerService.getCustomers(request.getStatus(), request.getPage(), request.getPageSize(), request.getSortBy(), request.getSortOrder());
        return ResponseEntity.ok(ApiResponse.success(customers));
    }

    /**
     * 根据ID获取客户信息
     * @param id 客户ID
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取客户详情", description = "根据ID获取客户详情")
    public ResponseEntity<ApiResponse<CustomerResponse>> getCustomerById(
            @Parameter(description = "客户ID", required = true, example = "1") @PathVariable Long id){
        CustomerResponse customer = customerService.getCustomerById(id);
        return ResponseEntity.ok(ApiResponse.success(customer));
    }

    /**
     * 创建客户
     * @param request 客户创建请求
     */
    @PostMapping
    @Operation(summary = "创建客户", description = "创建客户")
    public ResponseEntity<ApiResponse<CustomerResponse>> createCustomer(
            @Parameter(description = "客户创建请求", required = true) @RequestBody CustomerCreateRequest request ){
        CustomerResponse customer = customerService.createCustomer(request);
        return ResponseEntity.ok(ApiResponse.success(customer));
    }

    /**
     * 更新客户信息
     * @param id 客户ID
     * @param request 客户更新请求
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新客户信息", description = "更新客户信息")
    public ResponseEntity<ApiResponse<CustomerResponse>> updateCustomer(
            @Parameter(description = "客户ID", required = true, example = "1") @PathVariable Long id,
            @Parameter(description = "客户更新请求", required = true) @RequestBody CustomerUpdateRequest request ){
        CustomerResponse customer = customerService.updateCustomer(id, request);
        return ResponseEntity.ok(ApiResponse.success(customer));
    }

    /**
     * 删除客户
     * @param id 客户ID
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除客户", description = "删除客户")
    public ResponseEntity<ApiResponse<Void>> deleteCustomer(
            @Parameter(description = "客户ID", required = true, example = "1") @PathVariable Long id ){
        customerService.deleteCustomer(id);
        return ResponseEntity.ok(ApiResponse.success(null, "客户删除成功"));
    }
}
